package com.dd.ent.car.service;

import android.graphics.Bitmap;
import android.view.View;

import com.android.volley.Request;
import com.android.volley.Response;
import com.dd.ent.car.AppContext;
import com.dd.ent.car.common.http.BaseJsonObjectRequest;
import com.dd.ent.car.common.http.ImageLoadResponse;
import com.dd.ent.car.common.http.VolleyManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.pp.ent.Cherry;
import com.pp.ent.view.InjectUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by aqu-spring on 16/8/5.
 */
public class BaseService extends com.pp.ent.base.BaseService {
    protected final static String TAG = BaseService.class.getSimpleName();
    protected final static int PAGESIZE = 20;
    private VolleyManager volleyManager;

    public BaseService() {
        volleyManager = VolleyManager.instance(AppContext.getInstance());
        InjectUtils.injectObject(this);
    }

    protected void post(String url, Map<String, String> params, Response.Listener<JSONObject> respone, Response.ErrorListener errorListener) {
        BaseJsonObjectRequest jsonObjectRequest = new BaseJsonObjectRequest( url, params, respone, errorListener);
        volleyManager.addRequest(jsonObjectRequest);
    }

    protected void get(String url, Response.Listener<JSONObject> respone, Response.ErrorListener errorListener){
        BaseJsonObjectRequest jsonObjectRequest = new BaseJsonObjectRequest(url, respone, errorListener);
        volleyManager.addRequest(jsonObjectRequest);
    }

    /**
     * 加载网络图片，保存到磁盘
     *
     * @param url
     */
    protected void loadImage(String url, final ImageLoadResponse<String> imageLoadResponse) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true)
                .build();
        ImageLoader.getInstance().loadImage(url, null, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                imageLoadResponse.onLoadingStarted(imageUri, view);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                imageLoadResponse.onLoadingFailed(imageUri, view, failReason);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                imageLoadResponse.onLoadingComplete(imageUri, view, loadedImage, getFilePathByMd5(imageUri));
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                imageLoadResponse.onLoadingCancelled(imageUri, view);
            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {
                imageLoadResponse.onProgressUpdate(imageUri, view, current, total);
            }
        });
    }


    public String getFilePathByMd5(String url) {
        return ImageLoader.getInstance().getDiskCache().get(url).getPath();
    }


}
